package test;

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        Main instance = new Main();
//        instance.asterisk();
//        nextLine();
//        instance.lineHorizontal(8);
//        nextLine();
//        instance.lineVertical(5);
//        nextLine();
//        instance.triangle(3);
//        nextLine();
//        instance.diamond(5);
//        nextLine();
//        instance.diamondWithName(3, "Bill");
        instance.FizzBuzz();
        instance.generate(30);
    }

    /*
    *   to the next line.
    * */
    private static void nextLine() {
        System.out.println();
    }

    //        Easiest exercise ever
//        Print one asterisk to the console.
//        Example:
//        *
    private void asterisk() {
        System.out.print("*");
    }

    //    Draw a horizontal line
//    Given a number n, print n asterisks on one line.
//    Example when n=8:
//            ********
    private void lineHorizontal(int num) {
        if(num < 0) throw new IllegalArgumentException();
        while (num > 0) {
            asterisk();
            num--;
        }
    }

    //    Draw a vertical line
//    Given a number n, print n lines, each with one asterisks
//    Example when n=3:
//    *
//    *
//    *
    private void lineVertical(int num) {
        if(num < 0) throw new IllegalArgumentException();
        while (num > 0) {
            asterisk();
            nextLine();
            num--;
        }
    }

    //    Draw a right triangle
//    Given a number n, print n lines, each with one more asterisk than the last (i.e. one on the first line, two on the second,etc.)
//    Example when n=3:
//    *
//    **
//    ***
    private void triangle(int num) {
        if(num < 0) throw new IllegalArgumentException();
        for (int i = 0; i < num; i++) {
            lineHorizontal(i + 1);
            nextLine();
        }
    }

     ///////////////////////////////////////////////////////////////////////////////////////
    ////////   Diamond Exercises   ////////////////////////////////////////////////////////

    private static void indent(int num) {
        while(num > 0) {
            System.out.print(" ");
            num--;
        }
    }
//    Isosceles Triangle
//    Given a number n, print a centered triangle. Example for n=3:
//        *
//       ***
//      *****
    private void isoscelesTriangle(int num, int indent) {
        if(num < 0) throw new IllegalArgumentException();
        if(num == 1) asterisk();
        else
        for (int i=1; i <= num; i++) {
            indent(num-i+indent);
            lineHorizontal(i*2-1);
            nextLine();
        }
    }

    private void isoscelesTriangle2(int num, int indent) {
        if(num < 0) throw new IllegalArgumentException();
        for (int i=num; i > 0; i--) {
            indent(num-i+indent);
            lineHorizontal(i*2-1);
            nextLine();
        }
    }

//    Diamond
//    Given a number n, print a centered diamond. Example for n=3:
//
//       *
//      ***
//     *****
//      ***
//       *
    private void diamond(int num) {
        isoscelesTriangle(num, 0);
        isoscelesTriangle2(num-1,1);
    }


//    Diamond with Name
//    Given a number n, print a centered diamond with your name in place of the middle line. Example for n=3:
//
//     *
//    ***
//    Bill
//    ***
//     *
    private void diamondWithName(int num, String name) {
        isoscelesTriangle(num-1,1);
        System.out.println(name);
        isoscelesTriangle2(num-1,1);
    }


    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////   Fizz Exercise   ////////////////////////////////////

//    FizzBuzz Exercise
//
//    FizzBuzz is a simple number game where you count, but say "Fizz" and/or "Buzz" instead of numbers adhering to certain rules.
//
//    Create a FizzBuzz() method that prints out the numbers 1 through 100.
//    Instead of numbers divisible by three print "Fizz".
//    Instead of numbers divisible by five print "Buzz".
//    Instead of numbers divisible by three and five print "FizzBuzz".
    private void FizzBuzz() {
        for (int i=1;i<=100;i++) {
            if(i%15 == 0) System.out.println("FizzBuzz");
            else if(i%3 == 0 && i%5 != 0) System.out.println("Fizz");
            else if(i%3 != 0 && i%5 == 0) System.out.println("Buzz");
            else System.out.println(i);
        }
    }

//    Prime Factors Exercise
//
//    Write a method generate(int n) that given an integer N will return a list of integers such that the numbers are the factors of N and are arranged in increasing numerical order.
//
//    For example, generate(1) should return an empty list and generate(30) should return the numbers: 2,3,5.
    private void generate(int n) {
        ArrayList<Integer> numbers = new ArrayList<>();
        if(n%2 == 0) numbers.add(2);
        for (int i=3; i <= n; i += 2 ) {
            if(isPrime(i) && n%i==0){
                numbers.add(i);
            }
        }
        System.out.println(numbers);
    }

    private static boolean isPrime(int num) {
        for(int i=2;i<Math.sqrt(num);i++) {
            if(num%i == 0) return false;
        }
        return true;
    }
}
